<?php

namespace App\Console\Commands\SiteLocation;

use App\Console\Commands\Poi\BusinessPoi;
use App\Models\Wanggui\CustomerDepartmentTaskModel;
use App\Service\Common\AreaService;
use Illuminate\Console\Command;

class RepairDotPCD extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'siteLocation:repair-dot-pcd';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '调整勘址任务的省市区';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     */
    public function handle()
    {
        CustomerDepartmentTaskModel::query()
            ->where('provinceCode', 0)
            ->with(['siteLocationMallRel'])
            ->chunkById(30, function ($tasks) {
                foreach ($tasks as $task) {
                    if (null == $task->siteLocationMallRel) {
                        continue;
                    }
                    $administration = BusinessPoi::getAdministrationByCode($task->siteLocationMallRel->lng, $task->siteLocationMallRel->lat);

                    $regionInfo     = AreaService::getAdcodeByProvinceCityDistrict($administration['province'], $administration['city'], $administration['district']);

                    $task->siteLocationMallRel->province = $regionInfo['province']['name'];
                    $task->siteLocationMallRel->city     = $regionInfo['city']['name'];
                    $task->siteLocationMallRel->district = $regionInfo['district']['name'];
                    $task->siteLocationMallRel->save();

                    $this->info($regionInfo['province']['name'] . '-' . $regionInfo['city']['name'] . '-' . $regionInfo['district']['name']);

                    $task->prefixAddress = $regionInfo['province']['name'] . '-' . $regionInfo['city']['name'] . '-' . $regionInfo['district']['name'];
                    $task->provinceCode  = $regionInfo['province']['code'];
                    $task->cityCode      = $regionInfo['city']['code'];
                    $task->districtCode  = $regionInfo['district']['code'];
                    $task->save();
                }
            });


    }
}
